package com.sx.pipeline.ui.needs.construction.participation

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.OperatorNeedsListAdapter
import com.sx.pipeline.adapter.ParticipationNeedsListAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActParticipationNeedsBinding
import com.sx.pipeline.repos.http.res.ReleaseNeedsVO
import com.sx.pipeline.utils.AndroidUtil


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 施工单位-我参与的需求
 */
@Route(path = RoutePath.Mine.PARTICIPATION_NEEDS)
class ParticipationNeedsActivity :
    CYDataBindingBaseActivity<ActParticipationNeedsBinding, ParticipationNeedsVM>(R.layout.act_participation_needs),
    View.OnClickListener {
    private lateinit var mAdapter: ParticipationNeedsListAdapter

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mPage = 1
        mViewModel.takePartNeedPage("",mPage)
    }


    override fun initListener() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mPage++
                mViewModel.takePartNeedPage("",mPage)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mPage = 1
                mViewModel.takePartNeedPage("",mPage)
            }
        })
    }


    override fun initViewObservable() {
        super.initViewObservable()

        // 刷新事件
        observe(mViewModel.mUI.initRefreshEvent()) {
            mBinding.refreshLayout.finishRefresh(it!!)
        }

        // 加载事件
        observe(mViewModel.mUI.initLoadEvent()) {
            mBinding.refreshLayout.finishLoadMore(it!!)
        }

        // 需求列表列表
        setNeedsList()
    }

    private fun setNeedsList() {
        observe(mViewModel.releaseSupplyListEvent) {
            if (it != null && !it.records.isNullOrEmpty()) {
                mBinding.emptyView.visibility = View.GONE
                mBinding.recyclerView.visibility = View.VISIBLE
                if (!this::mAdapter.isInitialized) {
                    mBinding.recyclerView.layoutManager = LinearLayoutManager(
                        this, LinearLayoutManager.VERTICAL, false
                    )
                    mAdapter =
                        ParticipationNeedsListAdapter(it.records as MutableList<ReleaseNeedsVO>)
                    mBinding.recyclerView.adapter = mAdapter
                    mAdapter.setOnItemClickListener { adapter, view, position ->
                        val info = adapter.data[position] as ReleaseNeedsVO
                        //跳转需求详情
                        RouteManager.Mine.intentToOperatorNeedsDetail(info.id)
                    }
                    mAdapter.callCallBack = { phone: String ->
                        AndroidUtil.toCallPhone(phone, this)
                    }
                } else {
                    mBinding.refreshLayout.setEnableLoadMore(true)
                    if (mPage == 1) {
                        mAdapter.setNewInstance(it.records as MutableList<ReleaseNeedsVO>)
                    } else {
                        mAdapter.addData(it.records)
                    }
                }
                if (mAdapter.data.size == it.total) {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            } else {
                mBinding.refreshLayout.setEnableLoadMore(false)
                mBinding.emptyView.visibility = View.VISIBLE
                mBinding.recyclerView.visibility=View.GONE
            }
        }


    }

    override fun onClick(v: View?) {
        when (v?.id) {

        }
    }

}